package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.cache.UserCache;
import com.zzyl.dto.NursingProjectAddDto;
import com.zzyl.dto.NursingProjectUpdateDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    private final NursingProjectMapper nursingProjectMapper;

    public NursingProjectServiceImpl(NursingProjectMapper nursingProjectMapper) {
        this.nursingProjectMapper = nursingProjectMapper;
    }

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        Page<NursingProjectVo> page = PageHelper.startPage(pageNum, pageSize);
        List<NursingProjectVo> list = nursingProjectMapper.selectByPage(name, status);
        list.forEach(item -> {
            Long creator = item.getCreateBy();
            item.setCreator(creator != null ? UserCache.localCache.get(creator) : null);
            Long updater = item.getUpdateBy();
            item.setUpdater(updater != null ? UserCache.localCache.get(updater) : null);
        });
        PageResponse<NursingProjectVo> response = new PageResponse<>();
        response.setPage(page.getPageNum());
        response.setPages((long) page.getPages());
        response.setPageSize(page.getPageSize());
        response.setTotal(page.getTotal());
        response.setRecords(list);
        return response;
    }

    @Override
    public void add(NursingProjectAddDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.add(nursingProject);
        UserThreadLocal.remove();
    }

    @Override
    public NursingProject getById(Long id) {
        return nursingProjectMapper.getById(id);
    }

    @Override
    public void update(NursingProjectUpdateDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProject.setUpdateBy(1671403256519078138L);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setUpdateTime(LocalDateTime.now());
//        nursingProject.setUpdateBy(1671403256519078138L);
        nursingProject.setStatus(status);
        nursingProject.setId(Math.toIntExact(id));
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void delete(Long id) {
        nursingProjectMapper.delete(id);
    }

    @Override
    public List<NursingProject> getAll() {
        return nursingProjectMapper.getAll();
    }
}
